package com.cz.ssyx.cart.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.cz.ssyx.cart.service.CartInfoService;
import com.cz.ssyx.client.activity.ActivityFeignClient;
import com.cz.ssyx.common.auth.AuthContextHolder;
import com.cz.ssyx.common.result.Result;
import com.cz.ssyx.model.order.CartInfo;
import com.cz.ssyx.vo.order.OrderConfirmVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @descriptions:
 * @author: Joker.
 * @date: 2024/5/1 17:33
 * @version: 1.0
 */

@RestController
@RequestMapping("api/cart")
public class CartApiController {

    @Autowired
    private CartInfoService cartInfoService;

    @Autowired
    private ActivityFeignClient activityFeignClient;

    //添加商品到购物车
    @GetMapping("addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Long skuId, @PathVariable Integer skuNum) {
        Long userId = AuthContextHolder.getUserId();
        cartInfoService.addToCart(userId, skuId, skuNum);
        return Result.ok(null);
    }

    //根据skuId删除购物车
    @DeleteMapping("deleteCart/{skuId}")
    public Result deleteCart(@PathVariable("skuId") Long skuId) {
        Long userId = AuthContextHolder.getUserId();
        cartInfoService.deleteCart(userId, skuId);
        return Result.ok(null);
    }

    //清空购物车
    @DeleteMapping("deleteAllCart")
    public Result deleteAllCart(){
        Long userId = AuthContextHolder.getUserId();
        cartInfoService.deleteAllCart(userId);
        return Result.ok(null);
    }

    //批量删除购物车
    @DeleteMapping("batchDeleteCart")
    public Result batchDeleteCart(@RequestBody List<Long> skuIdList){
        Long userId = AuthContextHolder.getUserId();
        cartInfoService.batchDeleteCart(userId, skuIdList);
        return Result.ok(null);
    }

    //购物车列表
    @GetMapping("cartList")
    public Result cartList() {
        Long userId = AuthContextHolder.getUserId();
        List<CartInfo> cartInfoList = cartInfoService.cartList(userId);
        return Result.ok(cartInfoList);
    }

    //查询带有优惠券的购物车
    @GetMapping("activityCartList")
    public Result activityCartList() {
        Long userId = AuthContextHolder.getUserId();
        List<CartInfo> cartInfoList = cartInfoService.cartList(userId);
        OrderConfirmVo orderConfirmVo = activityFeignClient.findCartActivityAndCoupon(userId, cartInfoList);
        return Result.ok(orderConfirmVo);
    }

    //根据skuId设置购物车商品是否选中
    @GetMapping("checkCart/{skuId}/{isCheck}")
    public Result checkCart(@PathVariable Long skuId, @PathVariable Integer isCheck) {
        Long userId = AuthContextHolder.getUserId();
        cartInfoService.checkCartBySkuId(userId, skuId, isCheck);
        return Result.ok(null);
    }

    //全选
    @GetMapping("checkAllCart/{isCheck}")
    public Result checkAllCart(@PathVariable Integer isCheck) {
        Long userId = AuthContextHolder.getUserId();
        cartInfoService.checkAllCart(userId, isCheck);
        return Result.ok(null);
    }

    //批量选中
    @PostMapping("batchCartCheck/{isCheck}")
    public Result batchCartCheck(@RequestBody List<Long> skuIdList,
                                 @PathVariable Integer isCheck) {
        Long userId = AuthContextHolder.getUserId();
        cartInfoService.batchCartCheck(userId, skuIdList, isCheck);
        return Result.ok(null);
    }

    //获取购物车中选中的商品列表
    @GetMapping("inner/getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable Long userId) {
        return cartInfoService.getCartCheckedList(userId);
    }
}
